package com.gzdh.tang.dishrecognition.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.config.weixin.WXEntity;
import com.gzdh.tang.config.weixin.WXPay;
import com.gzdh.tang.config.weixin.WXPayUtils;
import com.gzdh.tang.modu.entity.MacMainpOrder;
import com.gzdh.tang.modu.entity.Reserve;
import com.gzdh.tang.modu.entity.ReserveXfjTemp;
import com.gzdh.tang.modu.service.MacMainpOrderService;
import com.gzdh.tang.modu.service.ReserveService;
import com.gzdh.tang.modu.service.ReserveXfjTempService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class DrTradeQueryJob {

    @Autowired
    private MacMainpOrderService macMainpOrderService;

    @Autowired
    private ReserveXfjTempService reserveXfjTempService;

    @Autowired
    private ReserveService reserveService;

    @XxlJob("DrTradeQueryJobHandler")
    public void tradeStatusQuery(){
        try {
            WXEntity wxEntity = new WXEntity();
            WXPay wxPay = new WXPay(wxEntity);
            Map<String, String> reqMap = new HashMap<>();
            if (WXPayUtils.getSubmchMode() == 1) {
                reqMap.put("sub_mch_id", WXPayUtils.getSubMchId());
            }
            QueryWrapper<MacMainpOrder> queryWrapper = new QueryWrapper<MacMainpOrder>()
                    .select("orders")
                    .in("notice_reserve", 0)
                    .groupBy("orders");
            for (MacMainpOrder macMainpOrder : macMainpOrderService.list(queryWrapper)) {
                String order = macMainpOrder.getOrders();
                if(reserveService.count(new QueryWrapper<Reserve>().eq("zfSerialnumber", macMainpOrder.getOrders())) > 0) {
                    macMainpOrderService.lambdaUpdate()
                            .set(MacMainpOrder::getNoticeReserve, 1)
                            .eq(MacMainpOrder::getOrders, order).update();
                    continue;
                }
                reqMap.put("out_trade_no", String.valueOf(order));
                Map<String, String> resMap = wxPay.orderQuery(reqMap);
                if(resMap == null){
                    log.error(order + ":查询超时");
                    continue;
                }
                if(!resMap.get("return_code").equals("SUCCESS")){
                    log.error(order + "(1):" + resMap.get("return_msg"));
                    continue;
                }
                if(!resMap.get("result_code").equals("SUCCESS")){
                    log.error(order + "(2):" + resMap.get("err_code_des"));
                    continue;
                }
                if(resMap.get("trade_state").equals("SUCCESS")){
                    List<ReserveXfjTemp> reserveXfjTempList = reserveXfjTempService.list(new QueryWrapper<ReserveXfjTemp>().eq("zfSerialnumber", order));
                    List<Reserve> reserveList = new ArrayList<>();
                    for (ReserveXfjTemp goo : reserveXfjTempList) {
                        Reserve reserve = new Reserve();
                        BeanUtils.copyProperties(goo, reserve);
                        reserveList.add(reserve);
                    }
                    reserveService.saveBatch(reserveList);
                    macMainpOrderService.lambdaUpdate().set(MacMainpOrder::getPayState, 1)
                            .set(MacMainpOrder::getNoticeReserve, 1)
                            .eq(MacMainpOrder::getOrders, order).update();
                }
            }
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
        }
    }
}
